package xyz.lne.researchgroupmanage.controller;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import xyz.lne.researchgroupmanage.annotation.AnonymousGetMapping;
import xyz.lne.researchgroupmanage.annotation.AnonymousPostMapping;
import xyz.lne.researchgroupmanage.common.CommonResponse;
import xyz.lne.researchgroupmanage.controller.request.AddDocumentRequest;
import xyz.lne.researchgroupmanage.controller.response.DocumentResponse;
import xyz.lne.researchgroupmanage.controller.response.DocumentSimpleResponse;
import xyz.lne.researchgroupmanage.controller.response.DocumentVersionResponse;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.security.UserPower;
import xyz.lne.researchgroupmanage.service.DocumentService;
import xyz.lne.researchgroupmanage.service.FileService;
import xyz.lne.researchgroupmanage.service.ResearchService;
import xyz.lne.researchgroupmanage.service.SysUserService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.DocumentDTO;
import xyz.lne.researchgroupmanage.service.dataTransferObject.DocumentSimpleDTO;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/document")
public class DocumentController {
    @Autowired
    FileService fileService;

    @Autowired
    DocumentService documentService;

    @Autowired
    ResearchService researchService;

    @Autowired
    SysUserService sysUserService;

    @AnonymousGetMapping(value = "/exist")
    public Boolean judgeExist(@RequestParam("researchId") Integer researchId, @RequestParam("documentName") String documentName) {
        if (documentService.findDocumentByDocumentName(researchId, documentName).isEmpty()) {
            return false;
        }
        return true;
    }

    @AnonymousPostMapping(value = "/add")
    public CommonResponse addDocument(@RequestBody AddDocumentRequest addDocumentRequest) {
        List<DocumentSimpleResponse> documentSimpleResponses = new LinkedList<>();

        try {
            documentService.addDocument(addDocumentRequest.getResearchId(), addDocumentRequest.getName(),
                    addDocumentRequest.getContent(), UserPower.check(UserPower.RESEARCH_JOIN, addDocumentRequest.getResearchId()),
                    addDocumentRequest.getRemark(), addDocumentRequest.getFileId(), 1);


            List<DocumentSimpleDTO> documentSimpleDTOS = documentService.findDocumentSimpleByResearchId(addDocumentRequest.getResearchId());

            documentSimpleDTOS.forEach(documentSimpleDTO -> {
                DocumentSimpleResponse documentSimpleResponse = new DocumentSimpleResponse();
                BeanUtils.copyProperties(documentSimpleDTO, documentSimpleResponse);
                documentSimpleResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
                documentSimpleResponse.setName(documentSimpleDTO.getDocumentName());
                documentSimpleResponses.add(documentSimpleResponse);
            });
            CommonResponse commonResponse = new CommonResponse();
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousPostMapping(value = "/addversion")
    public CommonResponse addDocumentversion(@RequestBody AddDocumentRequest addDocumentRequest) {

        DocumentResponse documentResponse = new DocumentResponse();

        try {
            Integer version = documentService.getNewestversion(addDocumentRequest.getResearchId(), addDocumentRequest.getName()) + 1;

            documentService.addDocument(addDocumentRequest.getResearchId(), addDocumentRequest.getName(),
                    addDocumentRequest.getContent(), addDocumentRequest.getUserId(),
                    addDocumentRequest.getRemark(), addDocumentRequest.getFileId(), version);

            DocumentDTO documentDTO = documentService.findDocumentByVersion(addDocumentRequest.getResearchId(), addDocumentRequest.getName(), version);

            BeanUtils.copyProperties(documentDTO, documentResponse);
            documentResponse.setUserName(sysUserService.getById(documentDTO.getUserId()).getName());
            documentResponse.setName(documentDTO.getDocumentName());

            CommonResponse commonResponse = new CommonResponse();
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }

        return CommonResponse.create(documentResponse);
    }

    @AnonymousPostMapping(value = "/delete")
    public CommonResponse deleteDocument(@RequestParam("documentName") String documentName,
                                         @RequestParam("researchId") Integer researchId) {
        List<DocumentSimpleResponse> documentSimpleResponses = new LinkedList<>();
        try {
            documentService.deleteDocument(researchId, documentName);

            List<DocumentSimpleDTO> documentSimpleDTOS = documentService.findDocumentSimpleByResearchId(researchId);

            documentSimpleDTOS.forEach(documentSimpleDTO -> {
                DocumentSimpleResponse documentSimpleResponse = new DocumentSimpleResponse();
                BeanUtils.copyProperties(documentSimpleDTO, documentSimpleResponse);
                documentSimpleResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
                documentSimpleResponse.setName(documentSimpleDTO.getDocumentName());
                documentSimpleResponses.add(documentSimpleResponse);
            });
            CommonResponse commonResponse = new CommonResponse();
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();
        }

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousPostMapping(value = "/deleteVer")
    public CommonResponse deleteDocumentVersion(
            @RequestParam("researchId") Integer researchId, @RequestParam("documentName") String documentName,
            @RequestParam("version") String version) {
        List<DocumentSimpleResponse> documentSimpleResponses = new LinkedList<>();
        try {
            Integer Version = 0;

            for (int i = 3; i < version.length() && version.charAt(i) != '.'; i++) {
                Version = Version * 10 + (version.charAt(i) - '0');
            }
            documentService.deleteDocumentversion(researchId, documentName, Version);

            List<DocumentSimpleDTO> documentSimpleDTOS = documentService.findDocumentSimpleByResearchId(researchId);


            documentSimpleDTOS.forEach(documentSimpleDTO -> {
                DocumentSimpleResponse documentSimpleResponse = new DocumentSimpleResponse();
                BeanUtils.copyProperties(documentSimpleDTO, documentSimpleResponse);
                documentSimpleResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
                documentSimpleResponse.setName(documentSimpleDTO.getDocumentName());
                documentSimpleResponses.add(documentSimpleResponse);
            });
            CommonResponse commonResponse = new CommonResponse();
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousGetMapping(value = "/list")
    public CommonResponse Document(@RequestParam("researchId") Integer researchId) {
        List<DocumentSimpleResponse> documentSimpleResponses = new LinkedList<>();
        List<DocumentSimpleDTO> documentSimpleDTOS;
        try {
            documentSimpleDTOS = documentService.findDocumentSimpleByResearchId(researchId);
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }
        documentSimpleDTOS.forEach(documentSimpleDTO -> {
            DocumentSimpleResponse documentSimpleResponse = new DocumentSimpleResponse();
            BeanUtils.copyProperties(documentSimpleDTO, documentSimpleResponse);
            documentSimpleResponse.setName(documentSimpleDTO.getDocumentName());
            documentSimpleResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
            documentSimpleResponses.add(documentSimpleResponse);
        });
        CommonResponse commonResponse = new CommonResponse();

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousGetMapping(value = "/version")
    public CommonResponse DocumentVersions(@RequestParam("researchId") Integer researchId, @RequestParam("doucmentName") String documentName) {
        List<DocumentVersionResponse> documentSimpleResponses = new LinkedList<>();
        List<DocumentSimpleDTO> documentSimpleDTOS;
        try {
            documentSimpleDTOS = documentService.findDocumentByDocumentName(researchId, documentName);
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }
        documentSimpleDTOS.forEach(documentSimpleDTO -> {
            DocumentVersionResponse documentVersionResponse = new DocumentVersionResponse();
            BeanUtils.copyProperties(documentSimpleDTO, documentVersionResponse);
            documentVersionResponse.setVersion("ver" + documentSimpleDTO.getVersion() + ".0");
            documentVersionResponse.setName(documentSimpleDTO.getDocumentName());
            documentVersionResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
            documentSimpleResponses.add(documentVersionResponse);
        });
        CommonResponse commonResponse = new CommonResponse();

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousGetMapping(value = "/search")
    public CommonResponse searchDocument(@RequestParam("researchId") Integer researchId,
                                         @RequestParam("documentName") String documentName) {
        List<DocumentSimpleResponse> documentSimpleResponses = new LinkedList<>();

        List<DocumentSimpleDTO> documentSimpleDTOS;
        try {

            documentSimpleDTOS = documentService.findDocumentByDocumentName(researchId, documentName);
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();

        }
        documentSimpleDTOS.forEach(documentSimpleDTO -> {
            DocumentSimpleResponse documentSimpleResponse = new DocumentSimpleResponse();
            BeanUtils.copyProperties(documentSimpleDTO, documentSimpleResponse);
            documentSimpleResponse.setUserName(sysUserService.getById(documentSimpleDTO.getUserId()).getName());
            documentSimpleResponse.setName(documentSimpleDTO.getDocumentName());
            documentSimpleResponses.add(documentSimpleResponse);
        });
        CommonResponse commonResponse = new CommonResponse();

        return CommonResponse.create(documentSimpleResponses);
    }

    @AnonymousGetMapping(value = "/detail")
    public CommonResponse showDocumentDetail(@RequestParam("researchId") Integer researchId,
                                             @RequestParam("documentName") String documentName,
                                             @RequestParam("version") String version) {
        DocumentResponse documentResponse = new DocumentResponse();

        Integer Version = 0;

        for (int i = 3; i < version.length() && version.charAt(i) != '.'; i++) {
            Version = Version * 10 + (version.charAt(i) - '0');
        }

        DocumentDTO documentDTO;
        try {
            documentDTO = documentService.findDocumentByVersion(researchId, documentName, Version);
        } catch (BadRequestException e) {
            System.out.println(e.getData());
            e.printStackTrace();
            return CommonResponse.Failed();
        }

        BeanUtils.copyProperties(documentDTO, documentResponse);
        documentResponse.setUserName(sysUserService.getById(documentDTO.getUserId()).getName());
        documentResponse.setResearchName(researchService.getById(documentDTO.getResearchId()).getName());
        documentResponse.setCreateDate(fileService.getById(documentDTO.getFileId()).getCreateDate());
        documentResponse.setFileName(fileService.getById(documentDTO.getFileId()).getFileName());
        documentResponse.setName(documentDTO.getDocumentName());
        documentResponse.setVersion("ver" + documentDTO.getVersion() + ".0");

        return CommonResponse.create(documentResponse);
    }

}

